
import java.io.InputStream;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author FFK
 */
public class playmusic {
    public  Player player;
    private VolumeControl volum;
    
        public void playfile(String nhac){
         try{
// Read wav file, packaged in the jar file
         InputStream in = getClass().getResourceAsStream("/"+nhac);
         String ctype=gettype(nhac);
         player = Manager.createPlayer(in,ctype);
         //player.setLoopCount(1);
         player.prefetch();
         player.realize();
         player.start();
         volum=(VolumeControl)player.getControl("VolumeControl");// dua volum cho thang nay dieu khien
         }
      catch (Exception e) {
	// Display an alert
          Alert alr = new Alert("Error", e.getMessage(), null, AlertType.ERROR);
          alr.setTimeout(Alert.FOREVER);
          //parent.setCurrent(alr,this);
       }
    }
        
         private static String gettype(String url)
        throws Exception {
        String type;

        // some simple test for the content type
        if (url.endsWith("wav")) {
            type = "audio/x-wav";
        } else if (url.endsWith("jts")) {
            type = "audio/x-tone-seq";
        } else if (url.endsWith("mid")) {
            type = "audio/midi";
        } else {
            throw new Exception("Cannot guess content type from URL: " + url);
        }

        return type;
    }
}
